/* This file is part of MyRename
 * Copyright (C) 2005-2009 Kai-Chieh Ku (kjackie@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "stdafx.h"
#include "resource.h"
#include "others.h"
#include "filename.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CString CFileName::folder;

CFileName::CFileName()
	: m_attribute(0), filename(NULL), title(NULL), ext(NULL)
{
}

CFileName::CFileName(LPCTSTR fn, uint16 attr /* = 0 */)
	: m_attribute(0), filename(NULL), title(NULL), ext(NULL)
{
	setFileName(fn, attr);
}

CFileName::CFileName(CFileName &fn)
	: m_attribute(0), filename(NULL), title(NULL), ext(NULL)
{
	if (this != &fn)
		*this = fn;
}

CFileName::~CFileName()
{
	clear();
}

void CFileName::clear()
{
	if (filename != NULL)
		delete [] filename;
	if (title != NULL)
		delete [] title;
	if (ext != NULL)
		delete [] ext;
	filename = NULL;
	title = NULL;
	ext = NULL;
}

LPCTSTR CFileName::GetName() const
{
	return filename;
}

LPCTSTR CFileName::GetTitle(const __int8 _case /* = 0*/)
{
	if (filename == NULL)
		return NULL;
	LPTSTR ptr = _tcsrchr(filename, _T('.'));
	if (isFolder() || ptr == NULL)
		return filename;

	if (title != NULL)
		delete [] title;
	title = new TCHAR [_tcslen(filename) + 1];

	switch (_case) {
		case 1:
			strToUpper(title, filename);
			break;
		case -1:
			strToLower(title, filename);
			break;
		default:
			_tcscpy(title, filename);
			break;
	}
	ptr = _tcsrchr(title, _T('.'));
	*ptr = _T('\0');
	return title;
}

LPCTSTR CFileName::GetType(const __int8 _case /* = 0*/)
{
	if (isFolder())
		return folder;
	if (filename == NULL)
		return NULL;

	LPTSTR ptr = filename;
	ptr = _tcsrchr(ptr, _T('.'));
	if (ptr != NULL) {
		ptr++;
		if (ext != NULL)
			delete [] ext;
		ext = new TCHAR [_tcslen(ptr) + 1];
		switch (_case) {
			case 1:
				strToUpper(ext, ptr);
				break;
			case -1:
				strToLower(ext, ptr);
				break;
			default:
				_tcscpy(ext, ptr);
				break;
		}
		return ext;
	}
	return NULL;
}

bool CFileName::isSystem() const
{
	return ((m_attribute & CFN_SYSTEM) != FALSE);
}

bool CFileName::isReadOnly() const
{
	return ((m_attribute & CFN_READONLY) != FALSE);
}

bool CFileName::isHidden() const
{
	return ((m_attribute & CFN_HIDDEN) != FALSE);
}

bool CFileName::isFolder() const
{
	return ((m_attribute & CFN_FOLDER) != FALSE);
}

void CFileName::setFileName(LPCTSTR fn, uint16 attr /* = 0 */)
{
	clear();
	filename = new TCHAR [_tcslen(fn) + 1];
	_tcscpy(filename, fn);
	m_attribute = attr;
}

CFileName::operator LPCTSTR()
{
	return filename;
}

CFileName& CFileName::operator =(CFileName &src)
{
	if (this != &src)
		setFileName(src, src.m_attribute);
	return *this;
}

CFileName& CFileName::operator =(LPCTSTR src)
{
	setFileName(src);
	return *this;
}
